<template>
  <div class="redirect">
    <div>redirect:跳转页面</div>
  </div>
</template>

<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import config from '@/config';
defineOptions({
  name: 'Redirect'
});

const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
router.isReady().then(() => {
  const storedToken = auth?.token ;
  if (config.loginType === 'token' as ('account' | 'authorization' | 'token' | 'key')) {
    const token = route.query.token;
    // 只要能获取到token就保存
    if (token) {
      auth.setToken(token as string);
    } else {
      // 未获取到token且本地也没有存储，终止逻辑
      if (!storedToken) {
        return;
      }
    }
  } else if (config.loginType === 'account' || config.loginType === 'authorization') {
    if (!storedToken) {
      router.push('/login');
      return;
    }
  } else if (config.loginType === 'key') {
    router.push(`/${config.layoutType === 'dashboard' ? 'dashboard' : 'main'}`);
    return;
  }
});
</script>

<style lang="scss" scoped>
.redirect {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 80vh;
  font-size: 32px;
}
</style>
